library(margins)
library(broom)
library(ggplot2)
library(haven)

##SWEDEN
df <- read_dta("merged_file_nomissings.dta")

# 1. dislike_socdem_diff ~ treat_flag
model_socdem_diff_flag <- lm(dislike_socdem_diff ~ treat_flag, data = df)
summary(model_socdem_diff_flag)


# Marginaleffekt för treat_flag i modellen dislike_socdem_diff
margins_socdem_diff_flag <- margins(model_socdem_diff_flag, variables = "treat_flag")
summary(margins_socdem_diff_flag)


# 2. dislike_socdem_diff ~ treat_cake
model_socdem_diff_cake <- lm(dislike_socdem_diff ~ treat_cake, data = df)
summary(model_socdem_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_socdem_diff
margins_socdem_diff_cake <- margins(model_socdem_diff_cake, variables = "treat_cake")
summary(margins_socdem_diff_cake)


# 3. dislike_vp_diff ~ treat_flag
model_vp_diff_flag <- lm(dislike_vp_diff ~ treat_flag, data = df)
summary(model_vp_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_vp_diff
margins_vp_diff_flag <- margins(model_vp_diff_flag, variables = "treat_flag")
summary(margins_vp_diff_flag)


# 4. dislike_vp_diff ~ treat_cake
model_vp_diff_cake <- lm(dislike_vp_diff ~ treat_cake, data = df)
summary(model_vp_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_vp_diff
margins_vp_diff_cake <- margins(model_vp_diff_cake, variables = "treat_cake")
summary(margins_vp_diff_cake)


# 5. dislike_mp_diff ~ treat_flag
model_mp_diff_flag <- lm(dislike_mp_diff ~ treat_flag, data = df)
summary(model_mp_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_mp_diff
margins_mp_diff_flag <- margins(model_mp_diff_flag, variables = "treat_flag")
summary(margins_mp_diff_flag)


# 6. dislike_mp_diff ~ treat_cake
model_mp_diff_cake <- lm(dislike_mp_diff ~ treat_cake, data = df)
summary(model_mp_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_mp_diff
margins_mp_diff_cake <- margins(model_mp_diff_cake, variables = "treat_cake")
summary(margins_mp_diff_cake)


# 7. dislike_cent_diff ~ treat_flag
model_cent_diff_flag <- lm(dislike_cent_diff ~ treat_flag, data = df)
summary(model_cent_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_cent_diff
margins_cent_diff_flag <- margins(model_cent_diff_flag, variables = "treat_flag")
summary(margins_cent_diff_flag)


# 8. dislike_cent_diff ~ treat_cake
model_cent_diff_cake <- lm(dislike_cent_diff ~ treat_cake, data = df)
summary(model_cent_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_cent_diff
margins_cent_diff_cake <- margins(model_cent_diff_cake, variables = "treat_cake")
summary(margins_cent_diff_cake)


# 9. dislike_lib_diff ~ treat_flag
model_lib_diff_flag <- lm(dislike_lib_diff ~ treat_flag, data = df)
summary(model_lib_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_lib_diff
margins_lib_diff_flag <- margins(model_lib_diff_flag, variables = "treat_flag")
summary(margins_lib_diff_flag)


# 10. dislike_lib_diff ~ treat_cake
model_lib_diff_cake <- lm(dislike_lib_diff ~ treat_cake, data = df)
summary(model_lib_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_lib_diff
margins_lib_diff_cake <- margins(model_lib_diff_cake, variables = "treat_cake")
summary(margins_lib_diff_cake)


# 11. dislike_mod_diff ~ treat_flag
model_mod_diff_flag <- lm(dislike_mod_diff ~ treat_flag, data = df)
summary(model_mod_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_mod_diff
margins_mod_diff_flag <- margins(model_mod_diff_flag, variables = "treat_flag")
summary(margins_mod_diff_flag)


# 12. dislike_mod_diff ~ treat_cake
model_mod_diff_cake <- lm(dislike_mod_diff ~ treat_cake, data = df)
summary(model_mod_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_mod_diff
margins_mod_diff_cake <- margins(model_mod_diff_cake, variables = "treat_cake")
summary(margins_mod_diff_cake)


# 13. dislike_kd_diff ~ treat_flag
model_kd_diff_flag <- lm(dislike_kd_diff ~ treat_flag, data = df)
summary(model_kd_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_kd_diff
margins_kd_diff_flag <- margins(model_kd_diff_flag, variables = "treat_flag")
summary(margins_kd_diff_flag)


# 14. dislike_kd_diff ~ treat_cake
model_kd_diff_cake <- lm(dislike_kd_diff ~ treat_cake, data = df)
summary(model_kd_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_kd_diff
margins_kd_diff_cake <- margins(model_kd_diff_cake, variables = "treat_cake")
summary(margins_kd_diff_cake)


# 15. dislike_sd_diff ~ treat_flag
model_sd_diff_flag <- lm(dislike_sd_diff ~ treat_flag, data = df)
summary(model_sd_diff_flag)

# Marginaleffekt för treat_flag i modellen dislike_sd_diff
margins_sd_diff_flag <- margins(model_sd_diff_flag, variables = "treat_flag")
summary(margins_sd_diff_flag)


# 16. dislike_sd_diff ~ treat_cake
model_sd_diff_cake <- lm(dislike_sd_diff ~ treat_cake, data = df)
summary(model_sd_diff_cake)

# Marginaleffekt för treat_cake i modellen dislike_sd_diff
margins_sd_diff_cake <- margins(model_sd_diff_cake, variables = "treat_cake")
summary(margins_sd_diff_cake)


#FLAG
# --- Bygg upp dataframen för treat_flag ---
coef_list <- list(
  broom::tidy(model_socdem_diff_flag)   %>% mutate(outcome = "S"),
  broom::tidy(model_vp_diff_flag)       %>% mutate(outcome = "V"),
  broom::tidy(model_mp_diff_flag)       %>% mutate(outcome = "MP"),
  broom::tidy(model_cent_diff_flag)     %>% mutate(outcome = "C"),
  broom::tidy(model_lib_diff_flag)      %>% mutate(outcome = "L"),
  broom::tidy(model_mod_diff_flag)      %>% mutate(outcome = "M"),
  broom::tidy(model_kd_diff_flag)       %>% mutate(outcome = "KD"),
  broom::tidy(model_sd_diff_flag)       %>% mutate(outcome = "SD")
)

coef_df_flag <- do.call(rbind, coef_list) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")

coef_df_flag <- coef_df_flag %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag ---

flag_d_se <- ggplot(coef_df_flag, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Party dislike by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold")
  )


ggsave(
  filename = "Flag dislike by party SE.jpeg",
  plot     = flag_d_se
)

# CAKE
# --- Bygg upp dataframen för treat_cake ---
coef_list_cake <- list(
  broom::tidy(model_socdem_diff_cake)   %>% mutate(outcome = "S"),
  broom::tidy(model_vp_diff_cake)       %>% mutate(outcome = "V"),
  broom::tidy(model_mp_diff_cake)       %>% mutate(outcome = "MP"),
  broom::tidy(model_cent_diff_cake)     %>% mutate(outcome = "C"),
  broom::tidy(model_lib_diff_cake)      %>% mutate(outcome = "L"),
  broom::tidy(model_mod_diff_cake)      %>% mutate(outcome = "M"),
  broom::tidy(model_kd_diff_cake)       %>% mutate(outcome = "KD"),
  broom::tidy(model_sd_diff_cake)       %>% mutate(outcome = "SD")
)

coef_df_cake <- do.call(rbind, coef_list_cake) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("S", "V", "MP", "C", "L", "M", "KD", "SD")

coef_df_cake <- coef_df_cake %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake ---

cake_d_se <- ggplot(coef_df_cake, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Cake: Party dislike by party, SE",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )


ggsave(
  filename = "Cake dislike by party SE.jpeg",
  plot     = cake_d_se
)


#####################################################
#DENMARK

#FLAG
# Förutsätter att dina data finns i en data.frame som heter `df`,
# och att variabeln treat_flag finns i df.

# 1. dislike_sociald_diff ~ treat_flag
model_sociald_diff_flag <- lm(dislike_sociald_diff ~ treat_flag, data = df)
summary(model_sociald_diff_flag)
margins_sociald_diff_flag <- margins(model_sociald_diff_flag, variables = "treat_flag")
summary(margins_sociald_diff_flag)

# 2. dislike_venstre_diff ~ treat_flag
model_venstre_diff_flag <- lm(dislike_venstre_diff ~ treat_flag, data = df)
summary(model_venstre_diff_flag)
margins_venstre_diff_flag <- margins(model_venstre_diff_flag, variables = "treat_flag")
summary(margins_venstre_diff_flag)

# 3. dislike_liberal_diff ~ treat_flag
model_liberal_diff_flag <- lm(dislike_liberal_diff ~ treat_flag, data = df)
summary(model_liberal_diff_flag)
margins_liberal_diff_flag <- margins(model_liberal_diff_flag, variables = "treat_flag")
summary(margins_liberal_diff_flag)

# 4. dislike_socfolke_n_diff ~ treat_flag
model_socfolke_diff_flag <- lm(dislike_socfolke_diff ~ treat_flag, data = df)
summary(model_socfolke_diff_flag)
margins_socfolke_diff_flag <- margins(model_socfolke_diff_flag, variables = "treat_flag")
summary(margins_socfolke_diff_flag)

# 5. dislike_danmarksd_diff ~ treat_flag
model_danmarksd_diff_flag <- lm(dislike_danmarksd_diff ~ treat_flag, data = df)
summary(model_danmarksd_diff_flag)
margins_danmarksd_diff_flag <- margins(model_danmarksd_diff_flag, variables = "treat_flag")
summary(margins_danmarksd_diff_flag)

# 6. dislike_moderat_diff ~ treat_flag
model_moderat_diff_flag <- lm(dislike_moderat_diff ~ treat_flag, data = df)
summary(model_moderat_diff_flag)
margins_moderat_diff_flag <- margins(model_moderat_diff_flag, variables = "treat_flag")
summary(margins_moderat_diff_flag)

# 7. dislike_konserv_diff ~ treat_flag
model_konserv_diff_flag <- lm(dislike_konserv_diff ~ treat_flag, data = df)
summary(model_konserv_diff_flag)
margins_konserv_diff_flag <- margins(model_konserv_diff_flag, variables = "treat_flag")
summary(margins_konserv_diff_flag)

# 8. dislike_enheds_folke_diff ~ treat_flag
model_enheds_folke_diff_flag <- lm(dislike_enheds_folke_diff ~ treat_flag, data = df)
summary(model_enheds_folke_diff_flag)
margins_enheds_folke_diff_flag <- margins(model_enheds_folke_diff_flag, variables = "treat_flag")
summary(margins_enheds_folke_diff_flag)

# 9. dislike_radikale_diff ~ treat_flag
model_radikale_diff_flag <- lm(dislike_radikale_diff ~ treat_flag, data = df)
summary(model_radikale_diff_flag)
margins_radikale_diff_flag <- margins(model_radikale_diff_flag, variables = "treat_flag")
summary(margins_radikale_diff_flag)

# 10. dislike_danskfolk_diff ~ treat_flag
model_danskfolk_diff_flag <- lm(dislike_danskfolk_diff ~ treat_flag, data = df)
summary(model_danskfolk_diff_flag)
margins_danskfolk_diff_flag <- margins(model_danskfolk_diff_flag, variables = "treat_flag")
summary(margins_danskfolk_diff_flag)

# 11. dislike_alternativ_diff ~ treat_flag
model_alternativ_diff_flag <- lm(dislike_alternativ_diff ~ treat_flag, data = df)
summary(model_alternativ_diff_flag)
margins_alternativ_diff_flag <- margins(model_alternativ_diff_flag, variables = "treat_flag")
summary(margins_alternativ_diff_flag)

# 12. dislike_nyeborg_diff ~ treat_flag
model_nyeborg_diff_flag <- lm(dislike_nyeborg_diff ~ treat_flag, data = df)
summary(model_nyeborg_diff_flag)
margins_nyeborg_diff_flag <- margins(model_nyeborg_diff_flag, variables = "treat_flag")
summary(margins_nyeborg_diff_flag)


# --- Bygg upp dataframen för treat_flag (_diff-modellerna) ---
coef_list_flag_diff <- list(
  broom::tidy(model_sociald_diff_flag)      %>% mutate(outcome = "A"),
  broom::tidy(model_venstre_diff_flag)      %>% mutate(outcome = "V"),
  broom::tidy(model_liberal_diff_flag)      %>% mutate(outcome = "I"),
  broom::tidy(model_socfolke_diff_flag)     %>% mutate(outcome = "F"),
  broom::tidy(model_danmarksd_diff_flag)    %>% mutate(outcome = "Æ"),
  broom::tidy(model_moderat_diff_flag)      %>% mutate(outcome = "M"),
  broom::tidy(model_konserv_diff_flag)      %>% mutate(outcome = "C"),
  broom::tidy(model_enheds_folke_diff_flag) %>% mutate(outcome = "Ø"),
  broom::tidy(model_radikale_diff_flag)     %>% mutate(outcome = "B"),
  broom::tidy(model_danskfolk_diff_flag)    %>% mutate(outcome = "O"),
  broom::tidy(model_alternativ_diff_flag)   %>% mutate(outcome = "Å"),
  broom::tidy(model_nyeborg_diff_flag)      %>% mutate(outcome = "D")
)

coef_df_flag_diff <- do.call(rbind, coef_list_flag_diff) %>%
  filter(term == "treat_flag") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("A", "V", "I", "F", "Æ", "M", "C", "Ø", "B", "O", "Å", "D")
coef_df_flag_diff <- coef_df_flag_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_flag (_diff) ---

flag_d_dk <- ggplot(coef_df_flag_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
    title = "Flag: Party dislike by party, DK",
    x = "",
    y = ""
  ) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Flag dislike by party DK.jpeg",
  plot     = flag_d_dk
)



#CAKE
# Förutsätter att dina data finns i en data.frame som heter `df`,
# och att variabeln treat_cake finns i df.

# 1. dislike_sociald_diff ~ treat_cake
model_sociald_diff_cake <- lm(dislike_sociald_diff ~ treat_cake, data = df)
summary(model_sociald_diff_cake)
margins_sociald_diff_cake <- margins(model_sociald_diff_cake, variables = "treat_cake")
summary(margins_sociald_diff_cake)

# 2. dislike_venstre_diff ~ treat_cake
model_venstre_diff_cake <- lm(dislike_venstre_diff ~ treat_cake, data = df)
summary(model_venstre_diff_cake)
margins_venstre_diff_cake <- margins(model_venstre_diff_cake, variables = "treat_cake")
summary(margins_venstre_diff_cake)

# 3. dislike_liberal_diff ~ treat_cake
model_liberal_diff_cake <- lm(dislike_liberal_diff ~ treat_cake, data = df)
summary(model_liberal_diff_cake)
margins_liberal_diff_cake <- margins(model_liberal_diff_cake, variables = "treat_cake")
summary(margins_liberal_diff_cake)

# 4. dislike_socfolke_n_diff ~ treat_cake
model_socfolke_diff_cake <- lm(dislike_socfolke_diff ~ treat_cake, data = df)
summary(model_socfolke_diff_cake)
margins_socfolke_diff_cake <- margins(model_socfolke_diff_cake, variables = "treat_cake")
summary(margins_socfolke_diff_cake)

# 5. dislike_danmarksd_diff ~ treat_cake
model_danmarksd_diff_cake <- lm(dislike_danmarksd_diff ~ treat_cake, data = df)
summary(model_danmarksd_diff_cake)
margins_danmarksd_diff_cake <- margins(model_danmarksd_diff_cake, variables = "treat_cake")
summary(margins_danmarksd_diff_cake)

# 6. dislike_moderat_diff ~ treat_cake
model_moderat_diff_cake <- lm(dislike_moderat_diff ~ treat_cake, data = df)
summary(model_moderat_diff_cake)
margins_moderat_diff_cake <- margins(model_moderat_diff_cake, variables = "treat_cake")
summary(margins_moderat_diff_cake)

# 7. dislike_konserv_diff ~ treat_cake
model_konserv_diff_cake <- lm(dislike_konserv_diff ~ treat_cake, data = df)
summary(model_konserv_diff_cake)
margins_konserv_diff_cake <- margins(model_konserv_diff_cake, variables = "treat_cake")
summary(margins_konserv_diff_cake)

# 8. dislike_enheds_folke_diff ~ treat_cake
model_enheds_folke_diff_cake <- lm(dislike_enheds_folke_diff ~ treat_cake, data = df)
summary(model_enheds_folke_diff_cake)
margins_enheds_folke_diff_cake <- margins(model_enheds_folke_diff_cake, variables = "treat_cake")
summary(margins_enheds_folke_diff_cake)

# 9. dislike_radikale_diff ~ treat_cake
model_radikale_diff_cake <- lm(dislike_radikale_diff ~ treat_cake, data = df)
summary(model_radikale_diff_cake)
margins_radikale_diff_cake <- margins(model_radikale_diff_cake, variables = "treat_cake")
summary(margins_radikale_diff_cake)

# 10. dislike_danskfolk_diff ~ treat_cake
model_danskfolk_diff_cake <- lm(dislike_danskfolk_diff ~ treat_cake, data = df)
summary(model_danskfolk_diff_cake)
margins_danskfolk_diff_cake <- margins(model_danskfolk_diff_cake, variables = "treat_cake")
summary(margins_danskfolk_diff_cake)

# 11. dislike_alternativ_diff ~ treat_cake
model_alternativ_diff_cake <- lm(dislike_alternativ_diff ~ treat_cake, data = df)
summary(model_alternativ_diff_cake)
margins_alternativ_diff_cake <- margins(model_alternativ_diff_cake, variables = "treat_cake")
summary(margins_alternativ_diff_cake)

# 12. dislike_nyeborg_diff ~ treat_cake
model_nyeborg_diff_cake <- lm(dislike_nyeborg_diff ~ treat_cake, data = df)
summary(model_nyeborg_diff_cake)
margins_nyeborg_diff_cake <- margins(model_nyeborg_diff_cake, variables = "treat_cake")
summary(margins_nyeborg_diff_cake)

# --- Bygg upp dataframen för treat_cake (_diff-modellerna) precis som i första fallet ---
coef_list_cake_diff <- list(
  broom::tidy(model_sociald_diff_cake)      %>% mutate(outcome = "A"),
  broom::tidy(model_venstre_diff_cake)      %>% mutate(outcome = "V"),
  broom::tidy(model_liberal_diff_cake)      %>% mutate(outcome = "I"),
  broom::tidy(model_socfolke_diff_cake)   %>% mutate(outcome = "F"),
  broom::tidy(model_danmarksd_diff_cake)    %>% mutate(outcome = "Æ"),
  broom::tidy(model_moderat_diff_cake)      %>% mutate(outcome = "M"),
  broom::tidy(model_konserv_diff_cake)      %>% mutate(outcome = "C"),
  broom::tidy(model_enheds_folke_diff_cake) %>% mutate(outcome = "Ø"),
  broom::tidy(model_radikale_diff_cake)     %>% mutate(outcome = "B"),
  broom::tidy(model_danskfolk_diff_cake)    %>% mutate(outcome = "O"),
  broom::tidy(model_alternativ_diff_cake)   %>% mutate(outcome = "Å"),
  broom::tidy(model_nyeborg_diff_cake)      %>% mutate(outcome = "D")
)

coef_df_cake_diff <- do.call(rbind, coef_list_cake_diff) %>%
  filter(term == "treat_cake") %>%
  select(outcome, estimate, std.error) %>%
  mutate(
    lower = estimate - 1.96 * std.error,
    upper = estimate + 1.96 * std.error
  )

# --- Definiera och tillämpa önskad ordning på outcome ---
ordered_outcomes <- c("A", "V", "I", "F", "Æ", "M", "C", "Ø", "B", "O", "Å", "D")
coef_df_cake_diff <- coef_df_cake_diff %>%
  mutate(outcome = factor(outcome, levels = rev(ordered_outcomes)))

# --- Rita horisontell koefficientplot för treat_cake (_diff) ---

cake_d_dk <- ggplot(coef_df_cake_diff, aes(x = outcome, y = estimate)) +
  geom_point(size = 2) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  coord_flip() +
  labs(
  title = "Cake: Party dislike by party, DK",
x = "",
y = ""
) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 12, face = "bold")
  )

ggsave(
  filename = "Cake dislike by party DK.jpeg",
  plot     = cake_d_dk
)
